package cn.edu.seu.ssny.controller.web;

import cn.edu.seu.ssny.common.R;
import cn.edu.seu.ssny.controller.BaseController;
import cn.edu.seu.ssny.entity.Enterprise;
import cn.edu.seu.ssny.entity.Project;
import cn.edu.seu.ssny.entity.User;
import cn.edu.seu.ssny.service.*;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@Slf4j
public class IndexController extends BaseController {

    @Autowired
    private IProjectService projectService;
    @Autowired
    private IStatisticsService statisticsService;


    @RequestMapping(value = {"/", "/index"})
    public String index(HttpSession session, ModelMap map) {
        User currUser = (User) session.getAttribute("currUser");
        Enterprise enterprise = (Enterprise) session.getAttribute("enterprise");
        List<Project> projects;
        if (currUser.getRole().getLevel() != IRoleService.SUPER_LEVEL) {
            projects = new ArrayList<>(currUser.getProjects());
        } else {
            projects = projectService.findAllByEnterprise(enterprise);
        }
        Collections.sort(projects);
        map.put("projects", projects);
        return "index";
    }

    /**
     * <功能描述:
     * 返回可在生能源利用量
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/index/data/renew_energy")
    public R getRenewEnergy() {
        Double renewEnergy = statisticsService.caculateRenewEnergy();
        Map<String, Object> data = new HashMap<>();
        data.put("renewEnergy", renewEnergy);
        return R.ok().data(data);
    }

    /**
     * <功能描述:
     * 返回节约标煤量
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/index/data/coal_saved")
    public R getCoalSaved() {
        Double coalSaved = statisticsService.caculateCoalSaved();
        Map<String, Object> data = new HashMap<>();
        data.put("coalSaved", coalSaved);
        return R.ok().data(data);
    }

    /**
     * <功能描述:
     * 返回碳排放量
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/index/data/carbon_emission")
    public R getCarbonEmission() {
        Double carbinEmission = statisticsService.caculateCarbonEmission();
        Map<String, Object> data = new HashMap<>();
        data.put("carbinEmission", carbinEmission);
        return R.ok().data(data);
    }


    /**
     * 功能描述:
     * <返回首页最下面的三条数据
     * <p>
     * 可再生能源利用量
     * 节约标煤量
     * 碳排放量
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/index/data/bottom")
    public R getIndexData() {
        Double carbinEmission = statisticsService.caculateCarbonEmission();
        Double coalSaved = statisticsService.caculateCoalSaved();
        Double renewEnergy = statisticsService.caculateRenewEnergy();
        Map<String, Object> data = new HashMap<>();
        data.put("carbinEmission", carbinEmission);
        data.put("coalSaved", coalSaved);
        data.put("renewEnergy", renewEnergy);
        return R.ok().data(data);
    }



}
